var req_loadregister;
function loadregister()
{
    var url = "Register.jsp?timee="+new Date().getTime();
    if(window.XMLHttpRequest)
    {
        req_loadregister = new XMLHttpRequest();
        req_loadregister.onreadystatechange=processloadregister;
        req_loadregister.open("GET",url,true);
        req_loadregister.send(null);
    }
    else if(window.ActiveXObject)
    {
        req_loadregister = new ActiveXObject("Microsoft.XMLHTTP");
        if(req_loadregister)
        {
            req_loadregister.onreadystatechange=processloadregister;
            req_loadregister.open("GET", url, true);
            req_loadregister.send(null);
        }
    }
}


function processloadregister()
{
    if(req_loadregister.readyState==4)
    {
        if(req_loadregister.status==200)
        {

            document.getElementById("content").innerHTML="";
            document.getElementById("content").innerHTML=req_loadregister.responseText;
        }

    }
}


var xmlHttp;
var xmlDOM;
var realpath = '${pageContext.request.contextPath}';

function GetXmlHttpObject(){
    var xmlHttp = null;
    try{
        xmlHttp = new XMLHttpRequest();
    } catch(e){
        try {
            xmlHttp = new ActiveXObject("Msxm12.XMLHTTP");
        } catch(e){
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}


function traversalDOMTree (fileName){
    //load file xml len bo nho o client
    var xmlDom = new ActiveXObject("Microsoft.XMLDOM");
    xmlDom.async = "false";
    xmlDom.load(fileName);

    if(xmlDom.parseError.errorCode != 0){
        alert("Error: " + xmlDom.parseError.reason);
    } else{
        flagSearchUsername=false;
        flagSearchMail = false;
        flagSearchPhone = false;
        flagEmptyUsername = false;
        flagEmptyPassword = true;
        flagEmptyCPassword = true;
        flagEmptyMail = false;
        flagEmptyPhone = false;
        flagMatchPass = true;
        flagValidEmail = true;
        //search tren client
        searchNode(xmlDom, document.getElementById("txtUsername").value, document.getElementById("txtMail").value, document.getElementById("txtPhone").value);
        //user
        if (!flagEmptyUsername)
        {
            if(flagSearchUsername)
            {
                document.getElementById("txtUser").innerHTML="";
                document.getElementById("txtUser").innerHTML="<font color='red'>Account was existing</font>";
            }
            else {
                document.getElementById("txtUser").innerHTML="";
            }
        } else {
            document.getElementById("txtUser").innerHTML="";
            document.getElementById("txtUser").innerHTML="<font color='red'>*</font>";
        }
        //ketthuc user

        var pass = document.getElementById("txtPassword").value;
        if(pass!=""){
            flagEmptyPassword=false;
        }
        if (!flagEmptyPassword)
        {
            document.getElementById("txtPass").innerHTML="";
        }
        else {
            document.getElementById("txtPass").innerHTML="";
            document.getElementById("txtPass").innerHTML="<font color='red'>*</font>";
        }


        var cpass = document.getElementById("cf_txtPassword").value;
        if(cpass!=""){
            flagEmptyCPassword=false;
        }


        if(cpass != pass){
            flagMatchPass = false;
            document.getElementById("txtRPass").innerHTML="<font color='red'>Password was not match</font>";
        } else document.getElementById("txtRPass").innerHTML="";



        var checkEmail = document.getElementById('txtMail');
        var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if(!flagEmptyMail){
            if(flagSearchMail)
            {
                document.getElementById("txtEmail").innerHTML="";
                document.getElementById("txtEmail").innerHTML="<font color='red'>Email was existing</font>";
            }
            else if (!filter.test(checkEmail.value)) {
                flagValidEmail = false;
                document.getElementById("txtEmail").innerHTML="";
                document.getElementById("txtEmail").innerHTML="<font color='red'>Email is not valid</font>";

            } else document.getElementById("txtEmail").innerHTML="";
        } else {
            document.getElementById("txtEmail").innerHTML="";
            document.getElementById("txtEmail").innerHTML="<font color='red'>*</font>";
        }



        if (!flagEmptyPhone){
            if(flagSearchPhone)
            {
                document.getElementById("txtPhoneNum").innerHTML="";
                document.getElementById("txtPhoneNum").innerHTML="<font color='red'>Phone number was existing</font>";
            }
            else {
                document.getElementById("txtPhoneNum").innerHTML="";
            }
        } else {
            document.getElementById("txtPhoneNum").innerHTML="";
            document.getElementById("txtPhoneNum").innerHTML="<font color='red'>*</font>";
        }

        if (!flagSearchPhone && !flagSearchMail && !flagSearchUsername && !flagEmptyPhone
            && !flagEmptyMail && !flagEmptyUsername && !flagEmptyPassword && !flagEmptyCPassword
            && flagMatchPass && flagValidEmail){
            document.getElementById("btRegister").disabled = false;
        }
        else
            document.getElementById("btRegister").disabled = true;
    }
}


var flagSearchUsername , flagSearchMail, flagSearchPhone;
var flagEmptyUsername, flagEmptyMail, flagEmptyPhone;
function searchNode(node, strSearchName, strSearchMail, strSearchPhone){
    if (node==null){
        return;
    }
    if (node.tagName == "checkRegister"){
        if (strSearchName != ""){
            var rgtN="";
            rgtN += node.firstChild.childNodes[0].nodeValue;

            if (rgtN == strSearchName){
                flagSearchUsername = true;
            }
        } else flagEmptyUsername = true;


        if (strSearchMail != ""){
            var sibling = node.childNodes[0].nextSibling;
            var rgtM="";
            rgtM += sibling.firstChild.nodeValue;

            if (rgtM == strSearchMail){
                flagSearchMail = true;
            }
        } else flagEmptyMail = true;


        if(strSearchPhone != ""){
            var sibling = node.childNodes[1].nextSibling;
            var rgtP="";
            rgtP += sibling.firstChild.nodeValue;

            if (rgtP == strSearchPhone){
                flagSearchPhone = true;
            }
        } else flagEmptyPhone = true;
    }

    var childs = node.childNodes;
    for (var i=0; i < childs.length; i++){
        searchNode(childs[i], strSearchName, strSearchMail, strSearchPhone);
    }
}


function reset1(){
    document.getElementById("txtUser").innerHTML=" ";
    document.getElementById("txtPass").innerHTML="";
    document.getElementById("txtRPass").innerHTML=" ";
    document.getElementById("txtFname").innerHTML="";
    document.getElementById("txtEmail").innerHTML="";
    document.getElementById("txtPhoneNum").innerHTML="";
    document.getElementById("txtAddr").innerHTML="";
    document.getElementById('txtUsername').value="";
    document.getElementById('txtPassword').value="";
    document.getElementById('cf_txtPassword').value="";
    document.getElementById('txtFullname').value="";
    document.getElementById('txtMail').value="";
    document.getElementById('txtPhone').value="";
    document.getElementById('txtAddress').value="";
}

